<template>
  <div>
    <el-container>
      <el-aside :width="asideWidth" style="min-height: 100vh;background-color: #001529;">
        <div
            class="logo">
          <img src="@/assets/logo.png" style="width: 30px; height: 30px">
          <transition name="el-fade-in-linear">
            <span
                style="font-size: 20px;font-family: 'Microsoft YaHei UI Light',serif;margin-left: 5px"
                v-show="!isCollapse">manager
            </span>
          </transition>
        </div>
        <el-menu
            :collapse="isCollapse"
            :collapse-transition="false"
            style="border: none;"
            :default-active="$route.path" router
            background-color="#001529"
            text-color="rgba(255,255,255,.65)" active-text-color="#fff">
          <el-menu-item index="/home">
            <i class="el-icon-s-home"></i>
            <span slot="title">系统首页</span>
          </el-menu-item>
          <el-menu-item index="/1">
            <i class="el-icon-s-goods"></i>
            <span slot="title">产品管理</span>
          </el-menu-item>
          <el-menu-item index="/2">
            <template>
              <i class="el-icon-s-management"></i>
              <span slot="title">人员管理</span>
            </template>
          </el-menu-item>
          <el-submenu index="/3" v-if="user.role === 'ADMIN'">
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>信息管理</span>
            </template>
            <el-menu-item index="/user">用户信息</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header>
          <i :class="collapseIcon" style="font-size: 26px" @click="handleCollapse()"></i>
          <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-left: 20px">
            <el-breadcrumb-item :to="{ path: '/home' }">主页</el-breadcrumb-item>
            <el-breadcrumb-item :to="{ path: $route.path }">{{ $route.meta.name }}</el-breadcrumb-item>
          </el-breadcrumb>

          <div class="loginInfo">
            <i class="el-icon-full-screen icons" @click="handleFullScreen"></i>
            <i class="el-icon-moon-night icons" @click="handleDarkMode"></i>
            <el-dropdown placement="bottom">
              <div style="display: flex; align-items: center;cursor:pointer;">
                <img :src="user.avatar || default_avatar" alt="" style="width: 40px;height: 40px; margin: 0 5px; border-radius: 50%">
                <span>{{ user.name }}</span>
              </div>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="$router.push('/info')">个人信息</el-dropdown-item>
                <el-dropdown-item @click.native="$router.push('/pwd')">修改密码</el-dropdown-item>
                <el-dropdown-item @click.native="exitCurrentUser">退出</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-header>
        <el-main>
          <router-view @update:user="updateUser"/>
        </el-main>
      </el-container>
    </el-container>
  </div>

</template>

<style>
.logo {
  display: flex;
  height: 60px;
  line-height: 60px;
  background-color: #001529;
  color: white;
  align-items: center;
  justify-content: center;
}

.el-menu--inline {
  background-color: #000c17 !important;
}

.el-menu--inline .el-menu-item {
  background-color: #000c17 !important;
  padding-left: 50px !important;
}

.el-submenu__title {
  line-height: 40px !important;
  height: 40px !important;
}

.el-menu-item:hover, .el-submenu__title:hover, .el-menu-item:hover i, .el-submenu__title:hover i {
  color: #fff !important;
  transition: .6s;
  background-color: #233443 !important;
  border-radius: 10px;
}

.el-menu-item.is-active {
  background-color: #1890ff !important;
  border-radius: 10px;
}

.el-menu-item {
  height: 40px !important;
  line-height: 40px !important;
}

.el-aside {
  transition: .3s;
  box-shadow: 2px 0 6px rgba(0, 0, 0, .7);
}

.el-header {
  box-shadow: 2px 0 6px rgba(0, 0, 0, .7);
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.loginInfo {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex: 1;
  width: 0;
  cursor: pointer;
}

.icons {
  font-size: 26px;
  margin-right: 10px;
}

</style>
<script lang="ts">

import request from "@/utils/request"

export default {
  name: 'HomeView',
  data() {
    return {
      isCollapse: false, // 不收缩
      asideWidth: '200px', // 变量控制侧边栏宽度
      collapseIcon: 'el-icon-s-unfold', // 控制收缩图标
      isFullScreen: false,
      users: {},
      isDark: false,
      user: JSON.parse(localStorage.getItem('honey-user') || '{}'),
      urls: [],
      default_avatar : 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
    }
  },
  mounted() {
    request.get('/user/list').then(res => {
      this.users = res.data;
    })
  },
  methods: {
    updateUser(user){ //获取子组件传过来的数据 更新当前页面的数据
      //让父级的对象跟子级的对象毫无关联
      this.user = JSON.parse(JSON.stringify(user))
    },
    handleCollapse() {
      this.isCollapse = !this.isCollapse
      this.asideWidth = this.isCollapse ? '64px' : '200px'
      this.collapseIcon = this.isCollapse ? 'el-icon-s-fold' : 'el-icon-s-unfold';
    },
    handleFullScreen() {
      if (this.isFullScreen) {
        document.exitFullscreen();
      } else {
        document.documentElement.requestFullscreen();
      }
      this.isFullScreen = !this.isFullScreen
    },
    handleDarkMode() {
      if ((this.isDark = !this.isDark)) {
        document.documentElement.classList.add('dark-mode')
        this.$message.success('切换主题成功')
      } else {
        document.documentElement.classList.remove('dark-mode')
        this.$message.success('切换主题成功')
      }
    },
    exitCurrentUser() {
      this.$router.push('/login')
      this.$message.success('成功退出当前账户')
      localStorage.removeItem('honey-user')
    },
    handleFileUpload(row,file){
      this.$set(row,'avatar',file.response.data)
      console.log(row)
      this.$request.post('/user/avatar',row).then(res =>{
        if(res.code === '200'){
          this.$message.success('上传成功')
        }else{
          this.$message.error(res.msg)
        }
      })
    },
    handleMultipleUpload(res,file,fileList){
      // console.log(res,file,fileList)
      this.urls = fileList.map(v => v.response?.data)
    },
    showUrls(){
      this.$message.success(this.urls)
    }
  }
}
</script>